package com.eleadmin.gps.utils;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @author Damon
 * @create 2021-08-20 15:21
 * 使用腾讯的接口获取的地理位置。
 *
 */
public class TxGetLocationUtill {

  public static final String getAddr(double[] location) {
//        double lat = location.getLatitude();
//        double log = location.getLongitude();
    double lat = location[0];
    double log = location[1];
    double latlong[] = TxLngLonUtil.gps84_To_Gcj02(lat, log);
    StringBuilder sb = new StringBuilder();
    InputStream in = null;
    BufferedReader reader = null;
    try {
      URL url = new URL("http://apis.map.qq.com/jsapi?qt=rgeoc&lnglat=" + latlong[1] + "," + latlong[0]);
      in = url.openStream();
      reader = new BufferedReader(new InputStreamReader(in, "GBK"));
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line);
      }
    } catch (Exception e) {
      System.out.println("URL" + e.getMessage());
    } finally {
      try {
        in.close();
        reader.close();
      } catch (Exception e) {
      }
    }
    Map result = (Map) new Gson().fromJson(sb.toString(), Map.class);
    System.out.println(result);
    Map info = (Map) result.get("info");
    String error = info.get("error").toString();
    String addr = "";
    if (error.equals("0.0")) {
      Map detail = (Map) result.get("detail");
      List poilist = (List) detail.get("poilist");
      Map p1 = (Map) poilist.get(0);
      addr = (String) p1.get("addr");
      String name = (String) p1.get("name");
      addr = addr + name;
    }
    return addr;
  }
}